<?php 
  require_once("backEnd/commons.php");
  sessionCheck();

?>
<!DOCTYPE html>
<html lang="es">
<head>
  <?php require_once("views/head.html"); ?>  
</head>
<body>
<div class="container-fluid">
  <div class="row">
    <div class="col-12 col-sm-1 col-lg-1"></div>
    <div class="col-12 col-sm-10 col-lg-10" style="background-color: #ccc; ">
      <?php require_once("views/nav.php"); ?>  
      <br>
      <img src="img/ubuntu-512.png" class="rounded mx-auto d-block" alt="logo" style="width: 120px;">
      <div class="display-6 text-center">GESTOR DE USUARIOS</div>
      <div class="text-muted text-center" style="text-transform: uppercase;">
        <?php echo (isset($_GET['msg']))? $_GET['msg']:"APLICACIONES WEB 21/22"; ?>
      </div>
      <br>
      <div class="row">
        <div class="col-md-3"></div>
        <div class="col-md-6">
          <form action="backEnd/userManager.php" method="post">
            <input type="hidden" name="op" value="new">
            <div class="mb-2">
              <input type="text" name="user" class="form-control form-control-sm" placeholder="Nombre de usuario" required>
            </div>
            <div class="mb-2">    
              <input type="password" name="pass" class="form-control form-control-sm" placeholder="Contraseña" required>
            </div>
            <div class="mb-2">    
              <input type="password" name="passCheck" class="form-control form-control-sm" placeholder="Repita contraseña" required>
            </div>
            <div class="mb-2">
              <input type="text" name="name" class="form-control form-control-sm" placeholder="Nombre" required>
            </div>
            <div class="mb-2">
              <input type="text" name="surname" class="form-control form-control-sm" placeholder="Apellidos" required>
            </div>
            <input type="submit" value="Registrar" class="btn btn-dark fullWidth">
          </form>
          <br>
          <div class="text-center" style="text-transform: uppercase;">
            LISTADO DE USUARIOS
          </div>
          <table>
            <tr><th>usuario</th><th>nombre</th><th>apellidos</th><th>borrar</th><th>editar</th></tr>
          <?php
          // USER AUTHENTICATION PROCESS
          $query = "SELECT users.user, users.name, users.surname FROM users WHERE users.type=1 ORDER BY users.user";
          $result = executeQuery($query);
          while($row = mysqli_fetch_assoc($result)) {            
            $user = $row['user'];
            $name = $row['name'];
            $surname = $row['surname'];
            echo "<tr><td>$user</td><td>$name</td><td>$surname</td><td><a href='backEnd/userManager.php?op=del&id=$user'>borrar</a></td><td><a href='backEnd/userManager.php?op=edt&id=$user'>editar</a></td></tr>\n";
          }
          mysqli_free_result($result);
          ?>
          </table>
          <br>
          <br>
          <br>
          <?php 
            session_start();
            $userData = $_SESSION['userForEdition'];
          ?>
          <form action="backEnd/userManager.php" method="post">
            <input type="hidden" name="op" value="upd">
            <div class="mb-2">
              <input type="text" name="id" class="form-control form-control-sm" value="<?php echo $userData['user']?>" required readonly>
            </div>
            <div class="mb-2">
              <input type="text" name="name" class="form-control form-control-sm" value="<?php echo $userData['name']?>" required>
            </div>
            <div class="mb-2">
              <input type="text" name="surname" class="form-control form-control-sm" value="<?php echo $userData['user']?>" required>
            </div>
            <input type="submit" value="Actualizar" class="btn btn-dark fullWidth">
          </form>

        </div>
        <div class="col-md-3"></div>  
      </div>
    </div>
    <div class="col-12 col-sm-1 col-lg-1"></div>
  </div>
</div>
<script type="text/javascript" src="js/jquery-3.6.0.min.js"></script>
</body>
</html>


